##########
# R Info #
##########
version
#platform       x86_64-w64-mingw32          
#arch           x86_64                      
#os             mingw32                     
#system         x86_64, mingw32             
#status                                     
#major          3                           
#minor          6.0                         
#year           2019                        
#month          04                          
#day            26                          
#svn rev        76424                       
#language       R                           
#version.string R version 3.6.0 (2019-04-26)
#nickname       Planting of a Tree 

library(ggplot2)
library(ggpubr)
library(maps)
library(wesanderson)
library(stargazer)
library(dplyr)

sessionInfo()
#R version 3.6.0 (2019-04-26)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 10 x64 (build 19041)
#Matrix products: default
#locale:
#[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
#[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
#[5] LC_TIME=English_United States.1252    
#attached base packages:
#[1] stats     graphics  grDevices utils     datasets  methods   base     
#other attached packages:
#[1] dplyr_1.0.4       stargazer_5.2.2   wesanderson_0.3.6 maps_3.3.0        ggpubr_0.4.0     
#[6] ggplot2_3.3.2    
#loaded via a namespace (and not attached):
#[1] zip_2.0.4         Rcpp_1.0.3        cellranger_1.1.0  pillar_1.4.3      compiler_3.6.0   
#[6] forcats_0.5.0     tools_3.6.0       lifecycle_0.2.0   tibble_3.0.4      gtable_0.3.0     
#[11] pkgconfig_2.0.3   rlang_0.4.10      openxlsx_4.1.4    DBI_1.1.0         rstudioapi_0.11  
#[16] curl_4.3          haven_2.2.0       rio_0.5.16        rJava_0.9-11      withr_2.4.1      
#[21] dplyr_1.0.4       hms_0.5.3         generics_0.0.2    xlsxjars_0.6.1    vctrs_0.3.6      
#[26] grid_3.6.0        tidyselect_1.1.0  data.table_1.12.8 glue_1.4.1        R6_2.4.1         
#[31] rstatix_0.6.0     readxl_1.3.1      foreign_0.8-71    carData_3.0-3     car_3.0-7        
#[36] purrr_0.3.3       tidyr_1.1.2       magrittr_1.5      backports_1.1.5   scales_1.1.0     
#[41] ellipsis_0.3.0    abind_1.4-5       assertthat_0.2.1  colorspace_1.4-1  ggsignif_0.6.0   
#[46] xlsx_0.6.1        stringi_1.4.5     munsell_0.5.0     broom_0.7.5       crayon_1.3.4




#############
# Load Data #
#############
load("ExamplesJQD.RData")

colnames(examples3)
# Page.Name:        name of facebook page
# name:             actor name
# country:          country name
# date:             postind date
# Likes.at.Posting: number of page likes at posting
# Likes:            number of Likes
# Comments:         number of comments
# Shares:           number of shares
# Love:             number of Love reactions
# Wow:              number of Wow reactions
# Haha:             number of Haha reactions 
# Sad:              number of Sad reactions
# Angry:            number of Angry reactions
# totalreactions:   sum of Likes, Love, Wow, Haha, Sad, and Angry
# loveprop:         proportion of Love reactions (Love / totalreactions)
# angryprop:        proportion of Angry reactions (Angry / totalreactions)

summary(examples3)

unique(examples3$country)
# "Brazil" "Poland" "United States"

unique(examples3$Page.Name)
#"PSDB" "Prawo i Sprawiedliwość"                 
# "Representative Alexandria Ocasio-Cortez" "Congressman Ted Yoho"

unique(examples3$name)
# "Brazilian Social Democracy Party (PSDB)" "Law and Justice (PiS)"                  
# "Rep. Alexandria Ocasio-Cortez" "Rep. Ted Yoho"

# set colors
rgy <- wes_palette("Darjeeling1", 3)




################################
# Figure 1: Three Case Studies #
################################
# create brazil date-level data
brz <- examples3[which(examples3$name == "Brazilian Social Democracy Party (PSDB)"),]

brz2 <- unique(brz[,c("name", "date")])
brz2 <- brz2[order(brz2$name, brz2$date),]

brz2$avelove <- na.omit(as.vector(t(tapply(brz$loveprop, list(brz$name, brz$date), mean))))
brz2$aveangry <- na.omit(as.vector(t(tapply(brz$angryprop, list(brz$name, brz$date), mean))))

brzl <- brz2[,1:3]
colnames(brzl)[3] <- "prop"
brzl$Reaction <- "Love"

brza <- brz2[,c(1,2,4)]
colnames(brza)[3] <- "prop"
brza$Reaction <- "Angry"

brz3 <- rbind(brzl, brza)
brz3$Reaction <- factor(brz3$Reaction, levels=c("Love", "Angry"))

# figure 1 top panel
brzexample <- ggplot(data = brz3, 
                     aes(x = date, y = prop, colour = Reaction)) + 
  geom_vline(aes(xintercept=as.Date("2016-08-31")), lty=2) +
  geom_line(lwd=1.2) +
  scale_color_manual(breaks = c("Love", "Angry"), values = rgy[c(2:1)]) +
  facet_wrap(. ~ name, ncol = 1, scales = "free_x") +
  ggtitle(label = "A. Brazil: President Rousseff's Impeachment") +
  xlab("Post Date (August 16, 2016-September 15, 2016)") + 
  ylab("") +
  theme_bw() + 
  theme(plot.title = element_text(face = "bold"),
        axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 14.9),
        legend.position = c(0.05, 0.8),
        legend.background = element_rect(size=0.7, linetype="solid", colour="black"))


# create poland date-level data
pol <- examples3[which(examples3$name == "Law and Justice (PiS)"),]

pol2 <- unique(pol[,c("name", "date")])
pol2 <- pol2[order(pol2$name, pol2$date),]

pol2$avelove <- na.omit(as.vector(t(tapply(pol$loveprop, list(pol$name, pol$date), mean))))
pol2$aveangry <- na.omit(as.vector(t(tapply(pol$angryprop, list(pol$name, pol$date), mean))))

poll <- pol2[,1:3]
colnames(poll)[3] <- "prop"
poll$Reaction <- "Love"

pola <- pol2[,c(1,2,4)]
colnames(pola)[3] <- "prop"
pola$Reaction <- "Angry"

pol3 <- rbind(poll, pola)
pol3$Reaction <- factor(pol3$Reaction, levels=c("Love", "Angry"))

# figure 1 middle panel
polexample <- ggplot(data = pol3, 
                     aes(x = date, y = prop, colour = Reaction)) + 
  geom_vline(aes(xintercept=as.Date("2020-10-22")), lty=2) +
  geom_line(lwd=1.2) +
  scale_color_manual(breaks = c("Love", "Angry"), values = rgy[c(2:1)]) +
  facet_wrap(. ~ name, ncol = 1, scales = "free_x") +
  ggtitle(label = "B. Poland: New Abortion Rule") +
  xlab("Post Date (October 1, 2020-October 31, 2020)") + 
  ylab("Love and Angry Proportion (Daily Average)") +
  theme_bw() + 
  theme(plot.title = element_text(face = "bold"),
        axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        legend.position = "none")


# create us date-level data
us <- examples3[which(examples3$country == "United States"),]

us2 <- unique(us[,c("name", "date")])
us2 <- us2[order(us2$name, us2$date),]

us2$avelove <- na.omit(as.vector(t(tapply(us$loveprop, list(us$name, us$date), mean))))
us2$aveangry <- na.omit(as.vector(t(tapply(us$angryprop, list(us$name, us$date), mean))))

usl <- us2[,1:3]
colnames(usl)[3] <- "prop"
usl$Reaction <- "Love"

usa <- us2[,c(1,2,4)]
colnames(usa)[3] <- "prop"
usa$Reaction <- "Angry"

us3 <- rbind(usl, usa)
us3$Reaction <- factor(us3$Reaction, levels=c("Love", "Angry"))
us3$name <- factor(us3$name, levels=rev(unique(us3$name)))

# figure 1 bottom panel
usexample <- ggplot(data = us3, 
                    aes(x = date, y = prop, colour = Reaction)) + 
  geom_vline(data=subset(us3, name=="Rep. Alexandria Ocasio-Cortez"), 
             aes(xintercept=as.Date("2020-07-23")), lty=2) +
  geom_vline(data=subset(us3, name=="Rep. Ted Yoho"), 
             aes(xintercept=as.Date("2020-07-20")), lty=2) +
  geom_line(lwd=1.2) +
  scale_color_manual(breaks = c("Love", "Angry"), values = rgy[c(2:1)]) +
  facet_wrap(. ~ name, ncol = 2, scales = "free_x") +
  ggtitle(label = "C. United States: AOC vs. Yoho") +
  xlab("Post Date (July 1, 2020-July 31, 2020)") + 
  ylab("") +
  theme_bw() + 
  theme(plot.title = element_text(face = "bold"),
        axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        legend.title = element_text(size =13),
        legend.text = element_text(size =13),
        legend.position = "none")


# figure 1 all
ggarrange(brzexample, polexample, usexample, 
          ncol = 1, nrow = 3)
#ggsave("Examples1.pdf", width=12, height=12)
